package com.zzgele.qmzb.api.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.cnc.cloudv.bean.channel.requestBean.GetRecordVideoListRequestBean;
import com.cnc.cloudv.bean.common.ResponseBean;
import com.cnc.cloudv.live.channelManage.ChannelManage;
import com.zzgele.qmzb.api.core.AbstractService;
import com.zzgele.qmzb.api.model.VideoStorage;
import com.zzgele.qmzb.api.properties.QmzbProperties;
import com.zzgele.qmzb.api.service.VideoStorageService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * ali oss 视频存储实现类
 *
 * Created by e on 2017/11/3
 *
 * @author <a href="http://eregg.com">eregg</a>
 */
@Service(proxy="jdk")
@Component
@Transactional
public class AliOSSVideoStorageServiceImpl implements VideoStorageService{

    private Logger logger = LoggerFactory.getLogger(AliOSSVideoStorageServiceImpl.class);

    @Autowired
    private ChannelManage channelManage;

    @Autowired
    private QmzbProperties qmzbProperties;

    private Map<String, String> getVideoUrl(VideoStorage videoStorage){
        Map<String, String> videoIdUrlMap = new HashMap<>();
        GetRecordVideoListRequestBean videoListRequestBean = new GetRecordVideoListRequestBean();
        videoListRequestBean.setPullId(videoStorage.getVideoId());
        videoListRequestBean.setStartTime(videoStorage.getStartTime());
        videoListRequestBean.setEndTime(videoStorage.getEndTime());
        try {
            ResponseBean recordVideoList = channelManage.getRecordVideoList(videoListRequestBean);
            logger.info("getRecordVideoList videoStorage:{},recordVideoList:{}", JSONObject.fromObject(videoStorage), JSONObject.fromObject(recordVideoList));

            String videoId = "91FECE35FC7F6668993A128C2EBE59B2";
            String videoUrl = "http://zhuimengvod.8686c.com/live/201711/87bf5fe364f84b93a3574cd4e0e3c228_20171104155416/87bf5fe364f84b93a3574cd4e0e3c228_20171104155416.flv";
            videoIdUrlMap.put(videoId, videoUrl);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return videoIdUrlMap;
    }


    @Override
    public void storage(VideoStorage videoStorage) {
        logger.info(JSONObject.fromObject(qmzbProperties.getAli()).toString());
        Map<String, String> videoIdUrlMap = new HashMap<>();
        String videoId = "91FECE35FC7F6668993A128C2EBE59B2";
        String videoUrl = "http://zhuimengvod.8686c.com/live/201711/87bf5fe364f84b93a3574cd4e0e3c228_20171104155416/87bf5fe364f84b93a3574cd4e0e3c228_20171104155416.flv";
        videoIdUrlMap.put(videoId, videoUrl);

        //Map<String, String> videoIdUrlMap = getVideoUrl(videoStorage);

        Map<String, String> resultMap = getVideoUrl(videoStorage);

        // endpoint设置
        String endpoint = qmzbProperties.getAli().getEndpoint();
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建
        String accessKeyId = qmzbProperties.getAli().getAccessKeyId();
        String accessKeySecret = qmzbProperties.getAli().getAccessKeySecret();
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        for (Map.Entry<String, String> entity : videoIdUrlMap.entrySet()){
            try {
                InputStream inputStream = new URL(entity.getValue()).openStream();
                PutObjectResult putObjectResult = ossClient.putObject("qmzb-video", entity.getKey()+".flv", inputStream);
                resultMap.put(entity.getKey(), JSONObject.fromObject(putObjectResult).toString());
                logger.info("upload restult {}", JSONObject.fromObject(putObjectResult).toString());
            } catch (IOException e) {
                //发生异常
                logger.error(e.getMessage());
                resultMap.put(entity.getKey(), e.getMessage());
            }
        }

        //请求回调地址，通知调用方，处理结果

        // 关闭client
        ossClient.shutdown();
    }
}
